﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */


#region Using
using System;
using System.Text;
using System.Security.Cryptography;
#endregion

namespace NetBay.Core.Encryption
{

    /// <summary>
    /// Class that handle MD5 Encryption
    /// </summary>
    public class MD5EncryptorDecryptor
    {
        public static string EncodeBase(string p_sOriginalPassword)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;
            StringBuilder l_sbStringBuilder = new StringBuilder();

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(p_sOriginalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);
            foreach (Byte b in encodedBytes)
            {
                l_sbStringBuilder.Append(b.ToString("x2").ToLower());
            }
            //Convert encoded bytes back to a 'readable' string
            return l_sbStringBuilder.ToString();
        }

        /// <summary>
        /// Decodes the base.
        /// </summary>
        /// <param name="p_sOriginalPassword">The P_S original password.</param>
        /// <returns></returns>
        public static string DecodeBase(string p_sOriginalPassword)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;
            StringBuilder l_sbStringBuilder = new StringBuilder();

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(p_sOriginalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);
            foreach (Byte b in encodedBytes)
            {
                l_sbStringBuilder.Append(b.ToString("x2").ToLower());
            }
            //Convert encoded bytes back to a 'readable' string
            return l_sbStringBuilder.ToString();
        }

   
    }
}
